'use strict';

var crypto = require('crypto'),
  debug = require('debug'),
  log = debug('yozh:lib:log'),
  error = debug('yozh:lib:error'),
  verbose = debug('yozh:lib:verbose'),
  config = require('../config/config');

/**
 * crypto tool
 * @type {Object}
 */
var hmac = module.exports = {};

/**
 * hmac crypto
 * @param  {String} type  'md5|sha1'
 * @param  {String} key   my app secret key
 * @param  {String} value the value required to operate
 * @return {String} encrypted value
 */
hmac.encrypt = function(type, key, value) {
  var encryption = crypto.createHmac(type, key);
  return encryption.update(value).digest('hex');
};

/**
 * md5 hmac crypto
 * @param  {String} value the value required to operate
 * @return {String} encrypted value
 */
hmac.md5Encrypt = function(value) {
  return this.encrypt(config.md5, config.hmacKey, value);
};

/**
 * sha1 hmac crypto
 * @param  {String} value the value required to operate
 * @return {String} encrypted value
 */
hmac.sha1Encrypt = function(value) {
  return this.encrypt(config.sha1, config.hmacKey, value);
};
